/**
 * 
 */
package uk.co.cdal.cobweb.frontend.GUI;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.ImageIcon;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;

import uk.co.cdal.cobweb.backend.BackendImpl;
import uk.co.cdal.cobweb.frontend.Frontend;
import uk.co.cdal.cobweb.frontend.GUI.actionlisteners.AddNodeActionListener;

/**
 * @author seb
 *
 */
public class MenuBar extends JMenuBar {
	
	private Frontend frontend;
	
	private JMenu file;
	private JMenuItem open;
	private JMenuItem save;
	private JMenuItem rollback;
	
	private JMenu session;
	private JMenuItem host;
	private JMenuItem connect;
	private JMenuItem disconnect;
	private JMenuItem showChat;
	 
	private JMenu idea;
	private JMenuItem newNode;
	private JMenuItem linkTo;
	private JMenuItem remove;
	private JMenuItem addLink;
	private JMenuItem addImage;
	private JMenuItem options;
	
	private JMenu help;
	private JMenuItem reference;
	private JMenuItem about;
	
	private SpiderNode curSN;
	
	public MenuBar() {
		
		frontend = Frontend.getFrontend();
		
		// Make File Menu
		file = new JMenu("File");
		
		// Make File MenuItems
		open = new JMenuItem("Open", new ImageIcon("icons/folder.png"));
		save = new JMenuItem("Save", new ImageIcon("icons/disk.png"));
		rollback = new JMenuItem("Rollback", new ImageIcon("icons/arrow_turn_left.png"));
		
		// Make Session Menu
		session = new JMenu("Session");
		
		// Make Session MenuItems
		host = new JMenuItem("Host", new ImageIcon("icons/house_go.png"));
		connect = new JMenuItem("Connect", new ImageIcon("icons/connect.png"));
		disconnect = new JMenuItem("Disconnect", new ImageIcon("icons/disconnect.png"));
		showChat = new JMenuItem("Show Chat", new ImageIcon("icons/comments.png"));
		
		// Make Idea Menu
		idea = new JMenu("Idea");
		
		// Make Idea MenuItems
		newNode = new JMenuItem("New idea	Ctrl + N", new ImageIcon("icons/new.png"));
		linkTo = new JMenuItem("Link To...", new ImageIcon("icons/arrow_up.png"));
		remove = new JMenuItem("Remove", new ImageIcon("icons/cancel.png"));
		addLink = new JMenuItem("Add URL", new ImageIcon("icons/link_add.png"));
		addImage = new JMenuItem("Add Image", new ImageIcon("icons/image_add.png"));
		options = new JMenuItem("Options", new ImageIcon("icons/cog_edit.png"));
		
		// Make Help Menu
		help = new JMenu("Help");
		
		// Make Help MenuItems
		reference = new JMenuItem("Reference", new ImageIcon("icons/book.png"));
		about = new JMenuItem("About", new ImageIcon("icons/help.png"));
		
		// Add the Items to the relevent parents
		add(file);
		file.add(open);
		file.add(save);
		file.add(rollback);
		
		add(session);
		session.add(host);
		session.add(connect);
		session.add(disconnect);
		session.add(showChat);
		
		add(idea);
		idea.add(newNode);
		idea.add(linkTo);
		idea.add(remove);
		idea.add(addLink);
		idea.add(addImage);
		idea.add(options);
		
		add(help);
		help.add(reference);
		help.add(about);
		
		// The currently selected node is null
		curSN = null;
		
		// Set the items of the menu we don't wish the user to have access to to unuseable
		disconnect.setEnabled(false);
		idea.setEnabled(false);
		
		// Set the TTT of all menu items
		open.setToolTipText("Open a spider diagram from a file.");
		save.setToolTipText("Save this spider diagram to a file.");
		rollback.setToolTipText("Roll this spider diagram back to the saved version.");
		
		host.setToolTipText("Host a new session.");
		connect.setToolTipText("Connect to a session.");
		disconnect.setToolTipText("Disconnect from your current session.");
		showChat.setToolTipText("Hide / Show the chat panel.");
		 
		newNode.setToolTipText("Add a new idea to the diagram.");
		linkTo.setToolTipText("Create a link between the currently selected idea and another idea.");
		remove.setToolTipText("Remove the current idea.");
		addLink.setToolTipText("Add a URL to the selected idea.");
		addImage.setToolTipText("Add an image to the selected idea.");
		options.setToolTipText("Select options for the currently selected idea.");
		
		reference.setToolTipText("Click here for help using COBWEB.");
		about.setToolTipText("Click here for more information about COBWEB.");
		
		
		// Add actionlisteners
		// Add action listener
		host.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				host();
			}
		});
		connect.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				connect();
			}
		});
		disconnect.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				disconnect();
			}
		});
		showChat.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				showChat();
			}
		});
		
		newNode.addActionListener(new AddNodeActionListener());
	}
	
	/**
	 * A method to reflect whats happening in the view of the model
	 * @param n The currently selected SpiderNode null if none selected.
	 */
	public void updateIdea(SpiderNode n) {
		if(n == null) {
			idea.setEnabled(false);
			idea.setToolTipText("Select an idea to modify");
		}
		else {
			curSN = n;
			idea.setEnabled(true);
			idea.setToolTipText("");
		}
	}
	
	public void updateConnectMenuItems() {
		if(frontend.getBackend().isConnected()) {
			disconnect.setEnabled(true);
			connect.setEnabled(false);
			host.setEnabled(false);
			frontend.showChat(true);
		}
		else {
			disconnect.setEnabled(false);
			connect.setEnabled(true);
			host.setEnabled(true);
		}
	}
	
	// Methods relating to each menu item.
	
	private void open() {
		//TODO: Add open code here
	}
	
	private void save() {
		//TODO: Add save code here
	}
	
	private void rollback() {
		//TODO: add roll back code here
	}
	
	private void host() {
		frontend.setUsername();
		frontend.getBackend().bind();
		frontend.getBackend().setConnected(true);
		updateConnectMenuItems();
		frontend.newSysMessage("Going into hosting mode");
	}
	
	private void connect() {
		//TODO: Code to connect here
		frontend.setUsername();
		frontend.getBackend().bind("88.96.214.129");
		updateConnectMenuItems();
		frontend.newSysMessage("Connected to peers.");
		
	}
	
	private void disconnect() {
		frontend.getBackend().unbind();
		frontend.getBackend().setConnected(false);
		updateConnectMenuItems();
		frontend.newSysMessage("Exiting hosting mode");

		
	}
	
	private void showChat() {
		frontend.showChat(!frontend.isChatVisible());
	}
	 
	private void newNode() {
		
	}
	
	private void linkTo() {
		
	}
	
	private void remove() {
		
	}
	
	private void addLink() {
		
	}
	
	private void addImage() {
		
	}
	
	private void options() {
		
	}
	
	private void reference() {
		
	}
	
	private void about() {
		
	}
}
